package com.xyzh05.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xyzh05.dao.VipServiceDao;
import com.xyzh05.dao.UserDao;
import com.xyzh05.model.User;
import com.xyzh05.model.VipService;
import com.xyzh05.model.VipServiceVO;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.PageBean;

@Service("vipServiceManager")
@WebService(serviceName = "VipServiceService", endpointInterface = "com.xyzh05.service.VipServiceManager")
public class VipServiceManagerImpl extends GenericManagerImpl<VipService, Long> implements VipServiceManager {
    VipServiceDao vipServiceDao;
    UserDao userDao;
    
    @Autowired
    public VipServiceManagerImpl(VipServiceDao vipServiceDao, UserDao userDao) {
        super(vipServiceDao);
        this.vipServiceDao = vipServiceDao;
        this.userDao = userDao;
    }
    
    public void save(VipServiceVO vo){
    	VipService vipService = new VipService();    
    	vipService.setVipServiceId(AutoSpecialCaseNumber());
    	if(vo.getVipId() != null){
    		User vipUser = userDao.get(vo.getVipId());
    		vipService.setVip(vipUser);
    	}
    	if(vo.getEditorId() != null){
    		User editorUser = userDao.get(vo.getEditorId());
    		vipService.setEditor(editorUser);
    		vipService.setAdviser(editorUser);
    	}
    	vipService.setPayment(vo.getPayment());
    	vipService.setIspay(vo.getIspay());
    	vipService.setPrice(vo.getPrice());
    	vipService.setStartTime(vo.getStartTime());
    	vipService.setEndTime(vo.getEndTime());
    	vipService.setCreateTime(new Date());
    	vipService.setUpdateTime(new Date());
    	vipService.setStatus(Short.valueOf("0"));
    	vipServiceDao.save(vipService);
    }
    
	public PageBean findList(PageBean pageBean, VipService vipService){
		StringBuilder sb = new StringBuilder();
		List list = new ArrayList();
		sb.append(" from VipService vs where 1 = 1 and vs.status != 2 ");
		if(vipService != null){
			if(vipService.getAdviser() != null){
				sb.append(" and vs.adviser.id = " + vipService.getAdviser().getId());
			}else if(vipService.getEditor() != null){
				sb.append(" and vs.editor.id = " + vipService.getEditor().getId());
			}else if(vipService.getVip() != null){
				sb.append(" and vs.vip.id = " + vipService.getVip().getId());
			}
			if(vipService.getStatus() != null){
				sb.append(" and vs.status = " + vipService.getStatus());
			}
		}
		
		if(pageBean != null){
			if (pageBean.getSortCriterion() != null) {
				sb.append(" order by ");
				sb.append(pageBean.getSortCriterion()+" ");			
			}
			if (pageBean.getSortParamDirection() != null) {
				sb.append(pageBean.getSortParamDirection());			
			}
		}
		
		return vipServiceDao.getPageByHQL(sb.toString(), pageBean, list.toArray());
	}
	
	private String AutoSpecialCaseNumber(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(new Date());
		String dates = dateStr.replace("-", "");
		List<?> list = vipServiceDao.findQuery(" select max(vipServiceId) from VipService where vipServiceId like '"+dates+"%' ");
		Object obj = list.get(0);
		String number = "";
		if(obj == null){
			return dates+"01";
		}else{
			String vipServiceId = obj.toString();
			String subId = vipServiceId.substring(8);
			int n = (int)Integer.parseInt(subId);		
			if((n+1)<10){
				n =+ 1;
				number = "0" + n;
			}
			number = (n+1)+"";		 
		}
		return dates+number;
	}
	
	public Boolean isEditorService(Long id){
		List<?> list = vipServiceDao.findQuery(" select count(s.id) from VipService s where s.editor.id = ? and status = 0) ", id);
		Object obj = list.get(0);
		if(obj instanceof Long){
			Long l = (Long)obj;
			return l == 0L ? true : false;
		}else if(obj instanceof Integer){
			Integer i = (Integer)obj;
			return i == 0 ? true : false;
		}else{
			String s = obj.toString();
			Long l = Long.valueOf(s);
			return l == 0L ? true : false;
		}		
	}
	
	public Boolean isAdviserService(Long id){
		List<?> list = vipServiceDao.findQuery(" select count(s.id) from VipService s where s.adviser.id = ? and status = 0) ", id);
		Object obj = list.get(0);
		if(obj instanceof Long){
			Long l = (Long)obj;
			return l == 0L ? true : false;
		}else if(obj instanceof Integer){
			Integer i = (Integer)obj;
			return i == 0 ? true : false;
		}else{
			String s = obj.toString();
			Long l = Long.valueOf(s);
			return l == 0L ? true : false;
		}		
	}

}